/*
 * @Author: wuyuxin
 * @Date: 2022-04-16 00:53:37
 * @LastEditors: wuyuxin
 * @LastEditTime: 2022-05-05 11:06:17
 * @Description:
 */
import { createStore, Store, useStore as useVuexStore } from 'vuex'

import loginModule from './loginModule/loginModule'
import system from './system/system'
import product from './product/product'
import dashboard from './analysis/dashboard'
import { IStoreType } from './type'
import { getPageListData } from '@/api/main/system/system'

const store = createStore({
  state() {
    return {
      name: 'wuyuxin',
      age: 22,
      entireDepartment: [],
      entireRole: [],
      entireMenu: []
    }
  },
  getters: {},
  mutations: {
    changeEntireDepartment(state, list) {
      state.entireDepartment = list
    },
    changeEntireRole(state, list) {
      state.entireRole = list
    },
    changeEntireMenu(state, list) {
      state.entireMenu = list
    }
  },
  actions: {
    async getInitialDataAction({ commit }) {
      // 1.请求部门和角色数据
      const departmentResult = await getPageListData('/department/list', {
        offset: 0,
        size: 1000
      })
      const { list: departmentList } = departmentResult.data
      const roleResult = await getPageListData('/role/list', {
        offset: 0,
        size: 1000
      })
      const { list: roleList } = roleResult.data

      const menuResult = await getPageListData('/menu/list', {})
      const { list: menuList } = menuResult.data

      // 2.保存数据
      commit('changeEntireDepartment', departmentList)
      commit('changeEntireRole', roleList)
      commit('changeEntireMenu', menuList)
    }
  },
  modules: {
    loginModule,
    system,
    product,
    dashboard
  }
})

export function setupStore() {
  store.dispatch('loginModule/loadLocalLogin')
}

export function useStore(): Store<IStoreType> {
  return useVuexStore()
}

export default store
